package tq.com.tqteacher.message.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseTitleRecycleFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.message.activity.TQMsgActivity;
import tq.com.tqteacher.message.activity.TQNoticeActivity;
import tq.com.tqteacher.message.adapter.TQMessageAdapter;
import tq.com.tqteacher.message.model.TQNoticeModel;
import tq.com.tqteacher.message.model.TQUserMsg;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;

public class TQMessageFragment extends BaseTitleRecycleFragment {
    private TQMessageAdapter mMessageAdapter;
    private TQEventBus mEventBus;
    private int page = 1;

    public static TQMessageFragment instance(){
        return new TQMessageFragment();
    }
    @Override
    protected void initRecycleView(RecyclerView pRecyclerView) {
        super.initRecycleView(pRecyclerView);

        if(mMessageAdapter == null){
            mMessageAdapter = new TQMessageAdapter(getContext());
            mMessageAdapter.setClickListener(mClickListener);
        }

        pRecyclerView.setAdapter(mMessageAdapter);
        pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        pRecyclerView.setBackgroundColor(Color.WHITE);

        if(mEventBus == null){
            mEventBus = new TQEventBus(mEventLisnter);
            mEventBus.register();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        this.loadClazzUser();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mEventBus != null){
            mEventBus.unregister();
        }
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("成员");
        pHeader5View.getBackIcon().setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initRefresh(SmartRefreshLayout pRefreshLayout) {
        pRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData();
            }
        });

        pRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadData();
            }
        });

        pRefreshLayout.autoRefresh();
    }

    private TQMessageAdapter.IClickListener mClickListener = new TQMessageAdapter.IClickListener() {
        @Override
        public void clickNotice() {
            Intent intent = new Intent(TQMessageFragment.this.getContext(), TQNoticeActivity.class);
            startActivity(intent);
        }

        @Override
        public void clickMsg(TQUserMsg pUserMsg) {
            Intent intent = new Intent(TQMessageFragment.this.getContext(), TQMsgActivity.class);
            intent.putExtra("sendTo", pUserMsg.getUserInfo().getId());
            startActivity(intent);
        }
    };

    private void loadData(){

        int cls = TQLogin.getCurrent().getUserInfo().getClzid();

        TQNetInterface.getNotice(cls, 1, 1, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(mRefreshLayout.getState() == RefreshState.Loading){
                    mRefreshLayout.finishLoadMore();
                }else{
                    mRefreshLayout.finishRefresh();
                }

                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQMessageFragment.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                try {
                    JSONArray lArray = pNetResult.getData().getJSONArray("notice");
                    JSONObject lObject = lArray.getJSONObject(0);

                    TQNoticeModel lNoticeModel = new TQNoticeModel();
                    lNoticeModel.refresh(lObject);


                    mMessageAdapter.setNotice(lNoticeModel);
                }catch (Exception pE){
                    pE.printStackTrace();
                }
            }
        });
    }

    private TQEventBus.IEventLisnter mEventLisnter = new TQEventBus.IEventLisnter() {
        @Override
        public void onEvnet(TQEventBus.TQEventMessage msg) {
            if(msg.getOperator() == TQEventBus.TQEventMessage.Opertor.add
                    && msg.getInfo() != null && msg.getInfo() instanceof TQNoticeModel){
                mMessageAdapter.setNotice((TQNoticeModel) msg.getInfo());
            }
        }
    };

    private void loadClazzUser(){
        TQNetInterface.getClazzUser(new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQMessageFragment.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                JSONArray data = pNetResult.getData().optJSONArray("user");
                if(data == null) return;

                ArrayList<TQUserMsg> lUserMsgs = new ArrayList<>();

                for (int i = 0; i < data.length(); i++){
                    JSONObject item = data.optJSONObject(i);
                    if(item == null) continue;

                    TQUserMsg lUserMsg = new TQUserMsg();
                    lUserMsg.refresh(item);
                    lUserMsgs.add(lUserMsg);
                }

                mMessageAdapter.setTQUserMsgs(lUserMsgs);
            }
        });
    }

}
